﻿using System;
using System.Collections.Generic;

namespace SS.WPF.Toolkit.PropertyEditor.Comparer
{
    /// <summary>
    /// Compare 2 item by Category and Name
    /// </summary>
    public class ByCategoryThenByNameComparer : IComparer<Property>
    {
        public int Compare(Property x, Property y)
        {
            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
            {
                return 0;
            }
            if (ReferenceEquals(x, y))
            {
                return 0;
            }
            // First compare by Category
            var val = string.Compare(x.Category, y.Category, StringComparison.OrdinalIgnoreCase);
            // If equal Category, the continue compare by Name
            return val == 0 ? string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase) : val;
        }
    }
}
